<?php include "../public/header.php";?>
<?php include "../public/header_edit.php";?>
<link rel="stylesheet" type="text/css" href="/static/component/laytp/module/cityPicker/city-picker.css">

<body id="optPage">
<form class="layui-form layui-form-pane" id="commentForm">
    <div class="page-title"><div class="page-title-con"><span>编辑责任主体</span><div class="move_title"></div><?php include "../public/sub.php";?></div></div>
    <div class="main-container">

        <div class="block-first">
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label">项目名称</label>
                        <div class="layui-input-block">
                            <input type="text" name="name" id="name" placeholder="请输入" autocomplete="off" disabled class="layui-input disabled">
                        </div>
                    </div>
                </div>
            </div>
        </div>


        <div class="block-generic">
            <div class="layui-form-item height_auto">
                <div class="short_table_list" key="1">
                    <div class="opt-btn-group">
                        <div><span>责任主体</span></div>
                        <div>
                            <button type="button" onclick="ctrlv_data_open()" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn">粘贴新增</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_copy_row">复制行</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_add_row">插入行</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_del_row">删除行</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_del_all">删除全部</button>
                        </div>
                    </div>
                    <table class="layui-table" lay-size="sm">
                        <thead>
                        <tr>
                            <th width="45">序号</th>
                            <th><text>*</text>单位名称</th>
                            <th>单位类型</th>
                            <th>联系人</th>
                            <th>联系方式</th>
                            <th>地址</th>
                            <th>备注</th>
                        </tr>
                        </thead>
                        <tbody class="short_table_tbody" id="duty_list"></tbody>
                    </table>
                    <div class="listerror">提示：<span></span></div>
                </div>
            </div>
        </div>

    </div>
</form>
<script>
    var param_id = getQueryVariable('id') || 0;

    layui.use(['laytp', 'form'], function() {
        var form = layui.form, layer = layui.layer;
        render_input_attr();

        facade.ajax({type: 'get', url: ask_engin_duty, data:{id:param_id}}).done(function(res){
            if (res.code === 2000) {
                var data = res.data.data;
                $("#name").val(data.name);

                var listAddStr = '';
                listAddStr += '<tr>';
                listAddStr += '<td style="width: 45px">1</td>';
                listAddStr += '<td><input type="text" class="layui-input danname"></td>';
                listAddStr += '<td><input type="text" class="layui-input dantype"></td>';
                listAddStr += '<td><input type="text" class="layui-input name"></td>';
                listAddStr += '<td><input type="text" class="layui-input tel"></td>';
                listAddStr += '<td><input type="text" class="layui-input addr"></td>';
                listAddStr += '<td><input type="text" class="layui-input explain"></td>';
                listAddStr += '</tr>';
                short_table_opt_func(listAddStr,1);


                var list = res.data.list;
                if(list.length > 0){
                    var str = '';
                    for (var key in list){
                        var nums = parseInt(key)+1;
                        str += '<tr>';
                        str += '<td style="width: 45px">'+nums+'</td>';
                        str += '<td><input type="text" class="layui-input danname" value="'+list[key].danname+'"></td>';
                        str += '<td><input type="text" class="layui-input dantype" value="'+list[key].dantype+'"></td>';
                        str += '<td><input type="text" class="layui-input name" value="'+list[key].name+'"></td>';
                        str += '<td><input type="text" class="layui-input tel" value="'+list[key].tel+'"></td>';
                        str += '<td><input type="text" class="layui-input addr" value="'+list[key].addr+'"></td>';
                        str += '<td><input type="text" class="layui-input explain" value="'+list[key].explain+'"></td>';
                        str += '</tr>';
                    }
                    $("#duty_list").html(str);
                }

                render_from();
            }
        });



        form.on('submit(add)', function(data){
            var list = [''];
            try {
                $('#duty_list tr').each(function (i) {
                    var danname = $(this).find('.danname').val();
                    var dantype = $(this).find('.dantype').val();
                    var name = $(this).find('.name').val();
                    var tel = $(this).find('.tel').val();
                    var addr = $(this).find('.addr').val();
                    var explain = $(this).find('.explain').val();

                    var index = i + 1;
                    if(!danname && !dantype && !name && !tel && !addr && !explain){
                        return false;
                    }
                    if(!name){
                        throw new Error('第'+index+'行，请输入单位名称');
                    }

                    list.push({
                        danname:danname,
                        dantype:dantype,
                        name:name,
                        tel:tel,
                        addr:addr,
                        explain:explain,
                    });
                });
            } catch(e) {
                $(".listerror").find('span').text(e.message);
                $(".listerror").show();
                return false;
            }
            $(".listerror").hide();
            data.field.list = list;

            data.field.id = param_id;
            facade.ajax({url: ask_engin_duty, data:data.field}).done(function(res){
                if (res.code === 2000) {
                    dataPostTimeOut();
                }
            });

            return false;
        });

    });



    var ctrlv_field_name = [
        {field: 'danname', name:'单位名称'},
        {field: 'dantype', name:'单位类型'},
        {field: 'name', name:'联系人'},
        {field: 'tel', name:'联系方式'},
        {field: 'addr', name:'地址'},
        {field: 'explain', name:'备注'},
    ];
    function ctrlv_data_show(list) {
        if(list.length > 0){
            var nums = $("#duty_list tr").length;
            for (var key in list){
                var str = '';
                nums++;
                str += '<tr>';
                str += '<td style="width: 45px">'+nums+'</td>';
                str += '<td><input type="text" class="layui-input danname" value="'+list[key].danname+'"></td>';
                str += '<td><input type="text" class="layui-input dantype" value="'+list[key].dantype+'"></td>';
                str += '<td><input type="text" class="layui-input name" value="'+list[key].name+'"></td>';
                str += '<td><input type="text" class="layui-input tel" value="'+list[key].tel+'"></td>';
                str += '<td><input type="text" class="layui-input addr" value="'+list[key].addr+'"></td>';
                str += '<td><input type="text" class="layui-input explain" value="'+list[key].explain+'"></td>';
                str += '</tr>';
                $("#duty_list").append(str);
            }
        }
        render_date();
        render_from();
    }

</script>
</body>
</html>
